package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddBook implements IOPeration{
    @Override
    public void work(BookList bookList){
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入你要上架的书名:");
        String name0 = scan.next();
        System.out.println("请输入书号:");
        String id0 = scan.next();
        //判断id是否符合条件
        for (int i = 0; i < id0.length(); i++) {
            if(!(id0.charAt(i) >= '0' && id0.charAt(i) <= '9')){
                System.out.println("id不符合条件,上架失败");
                return;
            }
        }
        //判断id是否重复
        Book[] books = bookList.books.toArray(new Book[0]);
        for (int i = 0; i < books.length; i++) {
            if(books[i].getId().equals(id0)){
                System.out.println("该id重复,上架失败");
                return;
            }
        }
        System.out.println("请输入上架数量:");
        int num0 = scan.nextInt();
        //用于判断是否超过图书容量
        int Num = num0;
        for (int i = 0; i < books.length; i++) {
            Num += books[i].getNum();
        }
        if(Num > bookList.MAX_SIZE){
            System.out.println("超过书店图书容量,上架失败");
            return;
        }
        System.out.println("请输入作者:");
        String writer0 = scan.next();
        System.out.println("请输入类型:");
        String type0 = scan.next();
        System.out.println("请输入初始热度:");
        int hot0 = scan.nextInt();
        Book book = new Book(name0,id0,num0,writer0,type0,hot0);
        bookList.books.add(book);
        System.out.println("上架成功");
    }
}
